/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.AnsatController;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Jeppe
 */
public class LoginPanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;

    /**
     * Creates new form LoginPanel
     */
    public LoginPanel(MainGUI mainGUI) {
        initComponents();
        this.mainGUI = mainGUI;
    }

    public void setScreenShotBaggrund(ImageIcon imgIcon) {
        screenShotBaggrundLabel.setIcon(imgIcon);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        statusLabel = new javax.swing.JLabel();
        loginKnap = new javax.swing.JButton();
        kodeordLabel = new javax.swing.JLabel();
        brugerLabel = new javax.swing.JLabel();
        passwordTxt = new javax.swing.JPasswordField();
        brugerTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        screenShotBaggrundLabel = new javax.swing.JLabel();

        setOpaque(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        jPanel3.setOpaque(false);
        jPanel3.setLayout(null);

        jPanel2.setOpaque(false);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("Brugernavn: LYK");

        jLabel2.setText("Kodeord: 9007 ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        statusLabel.setForeground(new java.awt.Color(255, 0, 0));

        loginKnap.setText("Log ind");
        loginKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginKnapActionPerformed(evt);
            }
        });

        kodeordLabel.setText("Kodeord:");

        brugerLabel.setText("Brugernavn:");

        passwordTxt.setText("9007");
        passwordTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                passwordTxtKeyReleased(evt);
            }
        });

        brugerTxt.setText("LYK");
        brugerTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                brugerTxtKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(loginKnap)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(brugerLabel)
                                    .addComponent(kodeordLabel))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(brugerTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                                    .addComponent(passwordTxt))))
                        .addGap(0, 39, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(brugerLabel)
                    .addComponent(brugerTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodeordLabel)
                    .addComponent(passwordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginKnap)
                .addContainerGap())
        );

        jPanel3.add(jPanel2);
        jPanel2.setBounds(60, 40, 299, 221);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/baggrundOverlay.png"))); // NOI18N
        jPanel3.add(jLabel3);
        jLabel3.setBounds(0, 0, 420, 290);

        add(jPanel3);
        jPanel3.setBounds(180, 120, 420, 290);
        add(screenShotBaggrundLabel);
        screenShotBaggrundLabel.setBounds(0, 0, 800, 610);
    }// </editor-fold>//GEN-END:initComponents

    private void loginKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginKnapActionPerformed
        try {
            if (AnsatController.getInstance().login(brugerTxt.getText(), passwordTxt.getText())) {
                mainGUI.showPage("MenuPanel");
                statusLabel.setText("");
                setVisible(false);
                mainGUI.updateBanner();
            } else {
                statusLabel.setText("Brugernavn og adgangskode er forkert.");
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1006 \nFejl ved indlæsning af ansat. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "DatabaseTimeout", 0);
        }
    }//GEN-LAST:event_loginKnapActionPerformed

    private void brugerTxtKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_brugerTxtKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                if (AnsatController.getInstance().login(brugerTxt.getText(), passwordTxt.getText())) {
                    mainGUI.showPage("MenuPanel");
                    statusLabel.setText("");
                    setVisible(false);
                    mainGUI.updateBanner();
                } else {
                    statusLabel.setText("Brugernavn og adgangskode er forkert.");
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Fejl 1006 \nFejl ved indlæsning af ansat. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "DatabaseTimeout", 0);
            }
        }
    }//GEN-LAST:event_brugerTxtKeyPressed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        brugerTxt.requestFocus();
        brugerTxt.selectAll();
    }//GEN-LAST:event_formComponentShown

    private void passwordTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordTxtKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                if (AnsatController.getInstance().login(brugerTxt.getText(), passwordTxt.getText())) {
                    mainGUI.showPage("MenuPanel");
                    statusLabel.setText("");
                    setVisible(false);
                    mainGUI.updateBanner();
                } else {
                    statusLabel.setText("Brugernavn og adgangskode er forkert.");
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Fejl 1006 \nFejl ved indlæsning af ansat. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "DatabaseTimeout", 0);
            }
        }
    }//GEN-LAST:event_passwordTxtKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel brugerLabel;
    private javax.swing.JTextField brugerTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel kodeordLabel;
    private javax.swing.JButton loginKnap;
    private javax.swing.JPasswordField passwordTxt;
    private javax.swing.JLabel screenShotBaggrundLabel;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables
}
